<template>
  <div class="container">
    <el-card class="box-card" style="width: 100%">
      <!-- 头部 -->
      <div slot="header" class="clearfix">
        <div class="left" style="float: left">
          <span class="title-name">学科名称</span>
          <el-input
            v-model="page.subjectName"
            placeholder="请输入内容"
            style="width: 190px"
          />
          <el-button @click="clearFn">清除</el-button>
          <el-button type="primary" @click="search">搜索</el-button>
        </div>
        <el-button
          type="success"
          style="float: right"
          icon="el-icon-edit"
          @click="addSubjects"
          >新增学科</el-button
        >
      </div>
      <!-- 提示 -->
      <el-row class="info"
        ><i class="el-icon-info" /> 数据一共 {{ counts }} 条</el-row
      >
      <!-- 表格 -->
      <div class="form">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="index" label="序号" width="48" />
          <el-table-column prop="subjectName" label="学科名称" width="105" />
          <el-table-column prop="username" label="创建者" width="101" />
          <el-table-column prop="addDate" label="创建日期" width="160" />
          <el-table-column
            prop="isFrontDisplay"
            label="前台是否显示"
            width="101"
          />
          <el-table-column
            prop="twoLevelDirectory"
            label="二级目录"
            width="101"
          />
          <el-table-column prop="tags" label="标签" width="101" />
          <el-table-column prop="totals" label="题目数量" width="101" />
          <el-table-column label="操作" width="240">
            <template v-slot="{ row }">
            <el-button type="text" @click="$router.push('/subjects/directorys')"
              >学科分类</el-button
            >
            <el-button type="text" @click="$router.push('/subjects/tags')"
              >学科标签</el-button
            >
            <el-button type="text" @click="edit(row.id)">修改</el-button>
            <el-button type="text" @click="delFn(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
         <el-pagination
         class="Pagination"
         background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.page"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="page.pagesize"
      :total="counts"
      layout="prev, pager, next ,sizes, jumper">
    </el-pagination>
      </div>
    </el-card>
    <SubjectsAdd :dialogVisible.sync="dialogVisible" @addFn='getSubjectList()'/>
  </div>
</template>

<script>
import SubjectsAdd from '@/module-hmmm/components/subjects-add.vue'
import { list, remove } from '@/api/hmmm/subjects.js'
export default {
  data () {
    return {
      tableData: [],
      counts: 0,
      dialogVisible: false,
      page: {
        page: 1,
        pagesize: 10,
        subjectName: ''
      }
    }
  },
  components: {
    SubjectsAdd
  },
  created () {
    this.getSubjectList()
  },
  methods: {
    async getSubjectList () {
      const res = await list(this.page)
      // console.log(res)
      res.data.items.forEach((item) => {
        item.addDate = item.addDate.replace('T', ' ')
      })
      res.data.items.forEach((item) => {
        item.addDate = item.addDate.replace('.000Z', ' ')
      })
      this.tableData = res.data.items
      // console.log(res)
      this.counts = res.data.counts
    },
    addSubjects () {
      this.dialogVisible = true
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.page.pagesize = val
      this.getSubjectList()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.page.page = val
      this.getSubjectList()
    },
    // 删除功能
    async delFn (id) {
      // console.log(id)
      await remove({ id })
      // console.log(res)
      this.$message.success('删除成功')
      this.getSubjectList()
    },
    editFn (id) {
      console.log(id)
    },
    clearFn () {
      this.page.subjectName = ''
      this.getSubjectList()
    },
    search () {
      this.getSubjectList()
    }

  }
}
</script>

<style lang="less" scoped>
.text {
  font-size: 14px;
}
.item {
  margin-bottom: 18px;
}
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}
.box-card {
  width: 480px;
}
.left .title-name {
  font-size: 14px;
  color: #606266;
  font-weight: 700;
}
.left .el-input {
  margin-left: 10px;
}
.left .el-button {
  margin-left: 10px;
}
.info {
  height: 35px;
  line-height: 35px;
  background-color: #f4f4f5;
  color: #909399;
  padding-left: 10px;
}
.form {
  margin-top: 20px;
}
.el-table {
  ::v-deep .el-table__header-wrapper {
    th {
      background-color: #fafafa;
    }
  }
}
.Pagination{
  float: right;
  margin: 20px 0px;
}
</style>
